
var cardMgr = require("./cardMgr");

const CardDecor = cardMgr.CardDecor;


class ZJHCardMgr extends cardMgr.CardMgr {

    //顺子
    isStraight(cards) {
        var dict = {};
        var minValue = 99;
        var maxValue = 0;
        for (let i = 0; i < cards.length; i++) {
            let value = this.getCardValue(cards[i]);
            if (value < minValue) {
                minValue = value;
            }
            if (value > maxValue) {
                maxValue = value;
            }
            if (dict[value]) {  // 有对子或者豹子，肯定不是顺子
                return false;
            }
            dict[value] = 1;
        }
        
        if ((maxValue - minValue + 1) == cards.length) {    // A23 ~ JQK
            return true;
        }

        if (dict[12] && dict[13] && dict[1]) {  // QKA
            return true;
        }
        return false;
    }
    //同花
    isSameDecor(cards) {
        var decor;
        for (let i = 0; i < cards.length; i++) {
            let type = this.getCardDecor(cards[i]);
            if(decor == null){
                decor = type;
            }
            else if(type != decor){
                return false;
            }
        }
        return true;
    }

    // 同花顺
    isSameDecorStraight(cards) {
        if (!this.isSameDecor(cards)) {
            return false;
        }
        if (!this.isStraight(cards)) {
            return false;
        }
        return true;
    }

    // 豹子
    isLeopard(cards) {
        var firstValue;
        for (let i = 0; i < cards.length; i++) {
            let value = this.getCardValue(cards[i]);
            if(firstValue == null){
                firstValue = value;
            }
            else if(value != firstValue){
                return false;
            }
        }
        return true;
    }
    isSpecialCard(cards) {
        var dict = {};
        var decorDict = {}
        for (let i = 0; i < cards.length; ++i) {
            let decor = this.getCardDecor(cards[i]);
            if (decorDict[decor]) return false;
            decorDict[decor] = 1;

            let value = this.getCardValue(cards[i]);
            dict[value] = dict[value] ? dict[value] + 1 : 1;
        }
        if (!dict[2] || !dict[3] || !dict[5]) {
            return false;
        }
        return true;
    }

    getMixCardPoint(cards) {
        var dict = {};
        for (let i = 0; i < cards.length; ++i) {
            let value = this.getCardValue(cards[i]);
            dict[value] = dict[value] ? dict[value] + 1 : 1;
        }

        var point = 0;
        for (let i = 2; i <= 14; ++i) {
            let value = i > 13 ? i - 13 : i;
            while (dict[value] && dict[value] > 0) {
                --dict[value];
                point = point * 100 + i;
            }
        }
        return point;
    }

    isPair(cards) {
        var dict = {};
        for (let i = 0; i < cards.length; ++i) {
            let value = this.getCardValue(cards[i]);
            dict[value] = dict[value] ? dict[value] + 1 : 1;
        }
        for (let i = 1; i <= 13; ++i) {
            if (dict[value] >= 2) {
                return true;
            }
        }
        return false;
    }

    getPairPoint(cards) {
        var dict = {};
        for (let i = 0; i < cards.length; i++) {
            let value = this.getCardValue(cards[i]);
            dict[value] = dict[value] ? dict[value] + 1 : 1;
        }
        var pairValue;
        for (let i = 1; i <= 13; ++i) {
            if (dict[value] >= 2) {
                pairValue = value;
                break;
            }
        }
        if (pairValue == null) return 0;

        var indexArr = [];
        for (let i = 2; i <= 14; ++i) {
            if (i == pairValue) continue;
            if (dict[i] == null) continue;
            indexArr.push(i);
        }
        indexArr.push[pairValue];
        
        var point = 0;
        for (let i = 0; i < indexArr.length; ++i) {
            let j = indexArr[i];
            let value = j > 13 ? j - 13 : j;
            while (dict[value] && dict[value] > 0) {
                --dict[value];
                point = point * 100 + j;
            }
        }
        return point;
    }

    calculate(cards) {
        if (this.isLeopard(cards)) {
            return 9000000 + this.getMixCardPoint(cards);
        }
        if (this.isSameDecorStraight(cards)) {
            return 8000000 + this.getMixCardPoint(cards);
        }
        if (this.isSameDecor(cards)) {
            return 7000000 + this.getMixCardPoint(cards);
        }
        if (this.isStraight(cards)) {
            return 6000000 + this.getMixCardPoint(cards);
        }
        if (this.isPair(cards)) {
            return 5000000 + this.getPairPoint(cards);;
        }
        return this.getMixCardPoint(cards);
    }
    
    compare(cards1, cards2) {
        if (this.isSpecialCard(cards1) && this.isLeopard(cards2)) return 1;
        if (this.isSpecialCard(cards2) && this.isLeopard(cards1)) return -1;
        var point1 = this.calculate(cards1);
        var point2 = this.calculate(cards2);
        if (point1 > point2) {
            return 1;
        } else if (point1 == point2) {
            return 0;
        } else {
            return -1;
        }
    }
}

exports.ZJHCardMgr = ZJHCardMgr;